package isanfeng.tuxiaoer.rabbitmq.exchange.topictype;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class TopicTypeProducer {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1. 创建一个ConnectionFactory，并进行配置
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.56.101");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        // 2. 通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();

        // 3. 通过connection创建一个Channel
        Channel channel = connection.createChannel();

        // 4. 通过channel发送数据
        String exchangeName = "test_topic_exchange";
        String routingKey1 = "user.save";
        String routingKey2 = "user.update";
        String routingKey3 = "user.delete.abc";
        String msg = "hello direct rabbit mq!";
        System.out.println("生产端：" + msg);
        channel.basicPublish(exchangeName, routingKey1, null, (msg+routingKey1).getBytes());
        channel.basicPublish(exchangeName, routingKey2, null, (msg+routingKey2).getBytes());
        channel.basicPublish(exchangeName, routingKey3, null, (msg+routingKey3).getBytes());
        // 5. 关闭相关的连接
        channel.close();
        connection.close();
    }
}
